<template>
  <div id="search-results">
    <van-list
      v-model="loading"
      :finished="finished"
      finished-text="没有更多了"
      @load="onLoad"
    >
      <van-cell
        v-for="(item, index) in list"
        :key="index"
        :title="item.title"
      />
    </van-list>
  </div>
</template>
<script>
import { getSearchResults } from '@/api/search'
export default {
  name: 'search-results',
  data () {
    return {
      list: [],
      loading: false,
      finished: false,
      page: 1
    }
  },
  d () {},
  methods: {
    async onLoad () {
      const searchContent = this.searchValue
      if (!searchContent) {
        return
      }
      const { data } = await getSearchResults({
        q: searchContent,
        page: this.page
      })
      const results = data.data
      this.list.push(...results)
      this.loading = false
      // 判定是否还有数据
      if (results.length) {
        // 有，页数+1
        this.page++
      } else {
        // 没有停止继续请求
        this.finished = true
      }
    }
  },
  components: {},
  props: {
    searchValue: {
      type: String,
      required: true
    }
  },
  watch: {},
  computed: {},
  mounted () {}
}
</script>

<style lang="less" scoped>
#search-results {
  position: fixed;
  left: 0;
  right: 0;
  top: 54px;
  bottom: 0;
  overflow-y: auto;
}
</style>
